package org.nf.material.dao.impl;

import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;
import org.nf.db.util.result.ColumnHandler;
import org.nf.material.dao.MaterialDAO;
import org.nf.material.model.Material;
import org.nf.partner.model.Partner;
import org.nf.product.model.Product;
import org.nf.product.util.ConnUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 材料dao 接口实现类
 */
public class MaterialDaoImpl implements MaterialDAO {
    /**
     * 添加材料
     * @param material
     * @return
     */
    @Override
    public int insertMaterial(Material material) {
        //实例化SqlExecutor对象 提供添加操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql="insert into design_material(material_name,material_type,material_details,material_price,material_image,product_id,product_name) values(?,?,?,?,?,?,?)";
        return executor.executeUpdate(sql,material.getMaterialName(),material.getMaterialType(),material.getMaterialDetails(),material.getMaterialPrice(),material.getMaterialImage(),material.getProductId(),material.getProductName());
    }

    /**
     * 删除材料
     * @param materialId
     * @return
     */
    @Override
    public int daleMaterial(int materialId) {
        //实例化SqlExecutor对象 提供删除操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql="delete from design_material where material_id=?";
        return executor.executeUpdate(sql,materialId);
    }

    /**
     * 修改材料
     * @param material
     * @return
     */
    @Override
    public int updateMaterial(Material material) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql="update design_material set material_name=?,material_type=?,material_details=?,material_price=?,material_image=?,product_id=?,product_name=? where material_id=?";
        return executor.executeUpdate(sql,material.getMaterialName(),material.getMaterialType(),material.getMaterialDetails(),material.getMaterialPrice(),material.getMaterialImage(),material.getProductId(),material.getProductName());
    }

    /**
     * 名称查询
     * @param materialName
     * @return
     */
    @Override
    public List<Material> selectMaterialName(String materialName) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<Material> handler = new BeanListHandler<>(Material.class);
        String sql="select material_id,material_name,material_type,material_details,material_price,material_image,product_id,product_name from design_material LIKE '%'?'%'";
        return executor.executeQuery(sql,handler,materialName);
    }

    /**
     * 分页查询产品
     * @param material
     * @param numberOf
     * @param limit
     * @return
     */
    @Override
    public List<Material> listMaterialPage(Material material, int numberOf, int limit) {
        //实例化SqlExecutor对象 提供增删改查操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        //定义一个集合，存储动态sql中的占位符
        List<Object> params = new ArrayList<>();
        //用于sql拼接
        StringBuilder sql = new StringBuilder("select material_id,material_name,material_type,material_details,material_price,material_image,product_id,product_name");
        sql.append(" from design_material where 1=1");
        if (material !=null && material.getMaterialName()!= null && !"".equals(material.getMaterialName().trim())){
            sql.append(" and material_name like '%'?'%'");
            params.add(material.getMaterialName());
        }
        sql.append("limit ?,?");
        BeanListHandler<Material> handler = new BeanListHandler<>(Material.class);
        return executor.executeQuery(sql.toString(), handler, numberOf, limit);

    }

    /**
     * 查询全部
     * @param material
     * @return
     */
    @Override
    public List<Material> listMaterial(Material material) {
        //实例化SqlExecutor对象 提供添加操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<Material> handler = new BeanListHandler<>(Material.class);
        String sql = "select material_id,material_name,material_type,material_details,material_price,material_image,product_id,product_name from design_material";

        return executor.executeQuery(sql,handler);
    }

    /**
     * 查询总总记录数，用于计算总页数
     * @return
     */
    @Override
    public Long countMaterial() {
        //实例化SqlExecutor对象 提供添加操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql= "select count(*) from design_material";
        ColumnHandler<Long> handler = new ColumnHandler<>(1);
        return executor.executeQuery(sql,handler);
    }
}
